<?php

class imageResizer {
	
	var $deleteOriginal;
	
	function imageResizer($deleteOriginal = false) {
		$this->deleteOriginal = $deleteOriginal;
	}
	
	function crop($fullPath, $startX, $startY, $selectionW, $selectionH, $desiredWidth, $desiredHeight, $renameRegExp = '/(.*)\.(.{3,4})/', $renameReplacement = '$1_thumb.$2') {
		
		if (!$this->_ensureExistance($fullPath)) return false;
		$thumbName = $this->_rename($renameRegExp, $renameReplacement, $fullPath);
		list($origWidth, $origHeight, $type) = getimagesize($fullPath);
		$thumbimg = imagecreatetruecolor($desiredWidth,$desiredHeight);
		$img = $this->_createSrcImg($fullPath, $type);
		
		$scale = ($desiredWidth / $selectionW);
		$newWidth  = ceil($origWidth  * $scale);
		$newHeight = ceil($origHeight * $scale);
		
		imagecopyresampled($thumbimg, $img, 0, 0, $startX, $startY, $newWidth, $newHeight, $origWidth, $origHeight );
		
		imagedestroy($img);
		imagejpeg($thumbimg, $thumbName, 85);
		imagedestroy($thumbimg);
		
		if ($this->deleteOriginal && ($thumbName != $fullPath)) {
			@ unlink ($fullPath);
		}
		
		return $thumbName;
	}
	
	function resize($fullPath, $desiredWidth = false, $desiredHeight = false, $renameRegExp = '/(.*)\.(.{3,4})/', $renameReplacement = '$1_thumb.$2', $cutImage = true, $bgR = 0, $bgG = 0, $bgB = 0) {
		
		if (!$this->_ensureExistance($fullPath)) return false;
		$thumbName = $this->_rename($renameRegExp, $renameReplacement, $fullPath);
		list($origWidth, $origHeight, $type) = getimagesize($fullPath);
		
		$offset_x = $offset_y = 0;
		if(!$desiredHeight) {
			$ratio = ($desiredWidth / $origWidth);
			$dst_height = $desiredHeight = floor($origHeight * $ratio);
			$dst_width  = $desiredWidth;
		} elseif (!$desiredWidth) {
			$ratio = ($desiredHeight / $origHeight);
			$dst_height = $desiredHeight;
			$dst_width  = $desiredWidth = floor($origWidth * $ratio);
		} else {
			$ratio_x    = $desiredWidth / $origWidth;
			$ratio_y    = $desiredHeight / $origHeight;
			if ($cutImage) {
				$ratio_max  = max($ratio_x, $ratio_y);
				$ratio      = min($ratio_x, $ratio_y);
			} else {
				$ratio_max = $ratio = min($ratio_x, $ratio_y);
			}
			$dst_width  = floor($origWidth * $ratio_max);
			$dst_height = floor($origHeight * $ratio_max);
			$offset_x   = - (($dst_width - $desiredWidth) / 2);
			$offset_y   = - (($dst_height - $desiredHeight) / 2);
	  }
	  
		if ($ratio < 1) {
			$img = $this->_createSrcImg($fullPath, $type);
			if (!$img) return false;
			
			$thumbimg = imagecreatetruecolor($desiredWidth,$desiredHeight);
			$bgColor = imagecolorallocate($thumbimg, $bgR, $bgG, $bgB);
			imagefill($thumbimg, 0, 0, $bgColor);
			
			imagecopyresampled($thumbimg, $img, $offset_x, $offset_y, 0, 0, $dst_width, $dst_height, $origWidth, $origHeight);
			imagedestroy($img);
			imagejpeg($thumbimg, $thumbName, 85);
				
			imagedestroy($thumbimg);
		} else {
			copy($fullPath ,$thumbName);
		}
		
		if ($this->deleteOriginal && ($thumbName != $fullPath)) {
			@ unlink ($fullPath);
		}
		
		return $thumbName;
	}
	
	function _ensureExistance($fullPath) {
		if (!is_file($fullPath)) {
			trigger_error('Error opening file '. $fullPath, E_USER_ERROR);
			return false;
		}
		return true;
	}
	
	function _rename($renameRegExp, $renameReplacement, $fullPath) {
		$thumbName = preg_replace($renameRegExp, $renameReplacement, $fullPath);
		$thumbName = preg_replace('/(.*)\.(.{3,4})/', '$1.jpg', $thumbName); // mi assicuro che l'estensione sia .jpg
		return $thumbName;
	}
	
	function _createSrcImg($fullPath, $type) {
		switch ($type) {
			case (IMAGETYPE_JPG):
			case (IMAGETYPE_JPEG):
				$img = imagecreatefromjpeg($fullPath);
				break;
			case (IMAGETYPE_GIF):
				$img = imagecreatefromgif($fullPath);
				break;
			case (IMAGETYPE_PNG):
				$img = imagecreatefrompng($fullPath);
				break;
			default:
				trigger_error('Unsupported image type', E_USER_ERROR);
				return false;
				break;
		}
		return $img;
	}
	
}

?>